import {observable, action} from "mobx";
import {ListViewPage, SearchAdvanced} from "weaver-mobile-page";
import * as API from "../../api";

export default class myProjectPageStore {
    @observable searchValue = "";
    @observable chooseIndex = -1;
    @observable conditionReady = false;
    @observable search_resourceid="";
    @observable tabkey = {
        "0": "doing",
        "1": "frozen",
        "2": "complete",
    }
    @observable conditiontype = {
        "0": "mymanagerprj",
        "1": "search"
    }
    @observable listAd = new ListViewPage.Store({route: "/myprojectsingle", id: "0"});

    @observable searchAd = new SearchAdvanced.Store({route: "/myprojectsingle", id: "ad"});
    @observable conditionFetchOptions = {    //初始化高级搜索列表
        url: "/api/prj/moblie/project/getPrjSearchCondition",
        method: "GET",
        params: {"conditiontype": this.conditiontype[0]}
    }
    @action
    Init = (search_resourceid) => {
        this.search_resourceid = search_resourceid;
        this.onSearch();
    };
    @action
    changeSearchValue = (val) => {
        this.searchValue = val;
        this.searchAd.getData(this.conditionFetchOptions).then((form) => {
            this.conditionReady = true;
            form.updateField("prjname", val);
        });
    };

    @action
    onSearch = (notAdvanced = true) => {
        let list, params, searchVal;
        list = this.listAd;
        searchVal = this.searchValue;
        if (this.chooseIndex !== -1) {
            params = {
                tabkey: this.tabkey[this.chooseIndex]
            }
        }
        if(this.search_resourceid){
            params = {...params,search_resourceid:this.search_resourceid}
        }

        params = {...params, "conditiontype": this.conditiontype[0], pageindex: 1};

        if (notAdvanced) {
            //初始化高级搜索面板，并将普通搜索框的条件同步到高级搜索
            this.searchAd.getData(this.conditionFetchOptions).then((form) => {
                this.conditionReady = true;
                form.updateField("prjname", searchVal);
            });
            Object.assign(params, {"prjname": searchVal});
        } else {
            // 获取高级搜索参数
            const {formParams} = this.searchAd.form;
            if (formParams["prjname"] !== undefined) {
                this.searchValue = formParams["prjname"];
            }
            Object.assign(params, formParams);
        }

        // 清除列表数据
        list.clearData();
        // 带参数请求列表数据
        this.getProjectList(list, params)
    };
    @action
    getProjectList = (list, params) => {
        API.getProjectList(params).then(action(res => {
            list.getData({dataKey: res.datas});
        }))
    };

    @action
    chooseState = (opt) => {
        if (this.chooseIndex === opt.index) {
            this.chooseIndex = -1;
        } else {
            this.chooseIndex = opt.index;
        }
        this.onSearch();    //带参请求数据
    };

}
